package org.njyinghuo.sdk.bytedance.client.douyin;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.njyinghuo.sdk.bytedance.conf.Config;
import org.njyinghuo.sdk.bytedance.exception.DouYinException;

import java.util.List;

/**
 * @description:
 * @author: Hupengpeng
 * @time: 2021/7/3 14:55
 */
public class FollowingList {
    private String open_id;
    private int cursor;
    private int count;
    private String access_token;
    private String response;
    private FollowingList() {
    }

    public static FollowingList New() {
        return new FollowingList();
    }
    public FollowingList execute() {
        String url = StrUtil.format("{}{}?open_id={}&access_token={}&cursor={}&count={}",
                Config.API_HTTP_SCHEME + Config.API_HOST,
                Config.API_FOLLOWING_LIST,
                getOpen_id(),
                getAccess_token(),
                getCursor(),
                getCount());
        String rs = HttpUtil.get(url);
        response = rs;
        return this;
    }
    public List<FansList.FansListResponse> convert() {
        try {
            JSONObject obj = JSONUtil.parseObj(response);
            JSONObject data = obj.getJSONObject("data");
            if (data.getInt("error_code") == 0) {
                Boolean has_more = data.getBool("has_more");
                Integer total = data.getInt("total");
                Integer cursor = data.getInt("cursor");
                List<FansList.FansListResponse> fansList = JSONUtil.toList(data.getJSONArray("list"), FansList.FansListResponse.class);
                fansList.forEach(fansListResponse -> {
                    fansListResponse.setHas_more(has_more);
                    fansListResponse.setNextCursor(cursor);
                    fansListResponse.setTotal(total);
                });
                return fansList;
            }
        } catch (Exception e) {
            throw new DouYinException(StrUtil.format("{}:{}", e.getMessage(), response));
        }
        throw new DouYinException(response);
    }
    public String getOpen_id() {
        return open_id;
    }

    public FollowingList setOpen_id(String open_id) {
        this.open_id = open_id;
        return this;
    }

    public int getCursor() {
        return cursor;
    }

    public FollowingList setCursor(int cursor) {
        this.cursor = cursor;
        return this;
    }

    public int getCount() {
        return count;
    }

    public FollowingList setCount(int count) {
        this.count = count;
        return this;
    }

    public String getAccess_token() {
        return access_token;
    }

    public FollowingList setAccess_token(String access_token) {
        this.access_token = access_token;
        return this;
    }

    public String getResponse() {
        return response;
    }

    public FollowingList setResponse(String response) {
        this.response = response;
        return this;
    }
}
